﻿using System;
using Foxy.GridViewControls;
using System.Web.UI.WebControls;
using System.Collections;

namespace Foxy.WebControls
{
    public partial class GridView : System.Web.UI.WebControls.GridView
    {
        #region Constants
        private const string CheckBoxColumHeaderTemplate = "<input type='checkbox' hidefocus='true' id='{0}' name='{0}' {1} onclick='CheckAll(this)'>";
        private const string CheckBoxColumHeaderID = "{0}_HeaderButton";
        private static readonly object EventRowClick = new object();
        private static readonly object EventRowDoubleClick = new object();
        private static readonly object EventRowCheckBoxChecked = new object();
        #endregion

        protected virtual ArrayList AddCheckBoxColumn(ICollection columnList)
        {
            // Get a new container of type ArrayList that contains the given collection. 
            // This is required because ICollection doesn't include Add methods
            // For guidelines on when to use ICollection vs IList see Cwalina's blog
            ArrayList list = new ArrayList(columnList);

            string shouldCheck = "";
            string checkBoxID = String.Format(CheckBoxColumHeaderID, ClientID);
            if (!DesignMode)
            {
                object o = Page.Request[checkBoxID];
                if (o != null)
                {
                    shouldCheck = "checked=\"checked\"";
                }
            }

            CheckBoxItem field = new CheckBoxItem();
            field.HeaderText = String.Format(CheckBoxColumHeaderTemplate, checkBoxID, shouldCheck);
            if (this.CheckBoxHeader != null)
                field.HeaderText = this.CheckBoxHeader;
            field.ReadOnly = true;

            if (CheckBoxColumnIndex > list.Count){
                list.Add(field);
                CheckBoxColumnIndex = list.Count - 1;
            }
            else
                list.Insert(CheckBoxColumnIndex, field);

            return list;
        }

        protected virtual TableItemStyle GetRowStyleFromState(DataControlRowState state)
        {
            switch (state)
            {
                case DataControlRowState.Alternate:
                    return AlternatingRowStyle;
                case DataControlRowState.Edit:
                    return EditRowStyle;
                case DataControlRowState.Selected:
                    return SelectedRowStyle;
                default:
                    return RowStyle;
            }
        }
    }
}
